using System;
using AntEater.ComponentModel.Validation;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace AntEater.ComponentModel.BuildElements
{
    /// <summary>
    /// Provides the Import build element.
    /// </summary>
    [Serializable]
    [BuildElement("Import")]
    [OpenBuildFileAction("EvaluatedProjectPath","Open Build File")]
    [Icon(typeof(ImportBuildElement), "AntEater.ComponentModel.Resources.Import.ico")]
    public class ImportBuildElement : BuildElementBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ImportBuildElement"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public ImportBuildElement(BuildFileDefinition context)
            : base(context) {
            
            _importedRootBuildElement = this.Context.CreateNewBuildElement(typeof(RootBuildElement)) as RootBuildElement;
        }

        private string _condition;

        /// <summary>
        /// Gets or sets the condition.
        /// </summary>
        /// <value>The condition.</value>
        [BuildElementProperty("Condition", false, BuildElementPropertyType.Condition)]
        [Description("Optional attribute.\nCondition to be evaluated.")]
        [Category("MsBuild")]
        public string Condition {
            get { return _condition; }
            set {
                if (_condition != value) {
                    _condition = value;
                    OnPropertyChanged("Condition");
                }
            }
        }

        private string _project;

        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        [BuildElementProperty("Project", true, BuildElementPropertyType.String)]
        [Description("Required attribute.\nThe path of the project file to import.")]
        [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
        [Category("MsBuild")]
        public string Project {
            get { return _project; }
            set {
                if (_project != value) {
                    _project = value;
                    OnPropertyChanged("Project");
                }
            }
        }

        /// <summary>
        /// Gets the evaluated project path.
        /// </summary>
        /// <value>The evaluated project path.</value>
        [Description("The evaluted path of the project file to import.")]
        public string EvaluatedProjectPath {
            get { return this.ImportedRootBuildElement.BuildFilePath; }
        }

        private bool _loaded;

        /// <summary>
        /// Gets or sets a value indicating whether the imported has been loaded.
        /// </summary>
        /// <value><c>true</c> if loaded; otherwise, <c>false</c>.</value>
        [ReadOnly(true)]
        [Description("Indicates if the imported project file could be loaded.")]
        public bool Loaded {
            get { return _loaded; }
            set {
                if (_loaded != value) {
                    _loaded = value;
                    OnPropertyChanged("Loaded");
                }
            }
        }

        private IRootBuildElement _importedRootBuildElement;

        /// <summary>
        /// Gets or sets the imported project.
        /// </summary>
        /// <value>The imported project.</value>
        [Browsable(false)]
        public IRootBuildElement ImportedRootBuildElement {
            get { return _importedRootBuildElement; }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Validates the specified recursive.
        /// </summary>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns></returns>
        public override ReadOnlyCollection<ValidationResult> Validate(bool recursive) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public override object Clone() {
            ImportBuildElement newElement = base.Clone() as ImportBuildElement;

            if (this._condition != null) {
                newElement._condition = this._condition.Clone() as string;
            }
            if (this._importedRootBuildElement != null) {
                newElement._importedRootBuildElement = this._importedRootBuildElement.Clone() as IRootBuildElement;
            }
            newElement._loaded = this._loaded;
            if (this._project != null) {
                newElement._project = this._project.Clone() as string;
            }

            return newElement;
        }
    }
}
